home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / pixelc1a / modcd.bas < prev    next >
Encoding:
BASIC Source File  |  1999-07-30  |  5.5 KB  |  165 lines

  1. Attribute VB_Name = "modCD"
  2. Option Explicit
  3. '============================================================
  4. '== Author  : Richard Lowe
  5. '== Date    : July 99
  6. '== Contact : riklowe@hotmail.com
  7. '============================================================
  8. '== Desciption
  9. '==
  10. '== This module contains the CollisionDetect function
  11. '==
  12. '============================================================
  13. '== Version History
  14. '============================================================
  15. '== 1.0  28-July-99  RL  Initial Release.
  16. '============================================================
  17.  
  18. '------------------------------------------------------------
  19. 'Dimension variables
  20. '------------------------------------------------------------
  21. Dim r As Integer, c As Integer
  22.  
  23. Dim hNewBMP As Long
  24. Dim hPrevBMP As Long
  25. Dim tmpObj As Long
  26.  
  27. Dim hMemDC As Long
  28. Dim nRet As Long
  29.  
  30. Dim blnCollision As Boolean
  31.             
  32. Dim iMaskWidth As Integer
  33. Dim iMaskHeight As Integer
  34.  
  35. Dim iM1SrcX As Integer
  36. Dim iM1SrcY As Integer
  37.  
  38. Dim iM2SrcX As Integer
  39. Dim iM2SrcY As Integer
  40.  
  41. Dim iDestX As Integer
  42. Dim iDestY As Integer
  43.  
  44. Dim iStartBlankWidth As Integer
  45. Dim iStartBlankHeight As Integer
  46.  
  47. Function CollisionDetect(ByVal X1 As Integer, ByVal Y1 As Integer, picMask As PictureBox, ByVal X2 As Integer, ByVal Y2 As Integer, picMask1 As PictureBox, picBlank As PictureBox) As Boolean
  48. '============================================================
  49. '== Desciption
  50. '==
  51. '== Name    : CollisionDetect
  52. '==
  53. '== Inputs
  54. '== X1       X position in pixels of the first sprite mask
  55. '== Y1       Y position in pixels of the first sprite mask
  56. '== picMask  Picturebox Object of the first sprite mask
  57. '== X2       X position in pixels of the second sprite mask
  58. '== Y2       Y position in pixels of the second sprite mask
  59. '== picMask  Picturebox Object of the second sprite mask
  60. '== picBlank Picturebox Object of a blank sprite
  61. '==
  62. '== Returns
  63. '== TRUE     If The pixels of sprites intersect
  64. '== FALSE    If The pixels of sprites do not intersect
  65. '==
  66. '== Notes
  67. '== Remove or comment out the section of code marked *** in a real program
  68. '== It is only included here to dislay the contents of the memory DC
  69. '==
  70. '============================================================
  71.     
  72. '------------------------------------------------------------
  73. 'This section of code calculates the overlapping mask section
  74. 'size, and defines the X and Y coordinates to be used to copy
  75. 'from each of the sprites into the memory DC.
  76. '
  77. 'These calcs have to take into account the orientation of the
  78. 'two sprites
  79. '------------------------------------------------------------
  80.     
  81.     If X1 <= X2 Then
  82.         iMaskWidth = X1 + picMask.ScaleWidth - X2
  83.         iM1SrcX = picMask.ScaleWidth - iMaskWidth
  84.         iM2SrcX = 0
  85.         iDestX = 0
  86.         iStartBlankWidth = iMaskWidth
  87.     Else
  88.         iMaskWidth = X2 + picMask.ScaleWidth - X1
  89.         iM1SrcX = 0
  90.         iM2SrcX = picMask.ScaleWidth - iMaskWidth
  91.         iDestX = 0
  92.         iStartBlankWidth = iMaskWidth
  93.     End If
  94.     
  95.     If Y1 <= Y2 Then
  96.         iMaskHeight = Y1 + picMask.ScaleHeight - Y2
  97.         iM1SrcY = picMask.ScaleHeight - iMaskHeight
  98.         iM2SrcY = 0
  99.         iDestX = 0
  100.         iStartBlankHeight = iMaskHeight
  101.     Else
  102.         iMaskHeight = Y2 + picMask.ScaleHeight - Y1
  103.         iM1SrcY = 0
  104.         iM2SrcY = picMask.ScaleHeight - iMaskHeight
  105.         iDestX = 0
  106.         iStartBlankHeight = iMaskHeight
  107.     End If
  108.     
  109. '------------------------------------------------------------
  110. 'Create a memory DC
  111. '------------------------------------------------------------
  112.  
  113.     hMemDC = CreateCompatibleDC(Screen.ActiveForm.hdc)
  114.     hNewBMP = CreateCompatibleBitmap(Screen.ActiveForm.hdc, picMask.ScaleWidth, picMask.ScaleHeight)
  115.     hPrevBMP = SelectObject(hMemDC, hNewBMP)
  116.     
  117. '------------------------------------------------------------
  118. 'Blank the memory dc, and draw the two sprite sections into it.
  119. '------------------------------------------------------------
  120.  
  121.     BitBlt hMemDC, 0, 0, picBlank.ScaleWidth, picBlank.ScaleHeight, picBlank.hdc, 0, 0, vbNotSrcCopy
  122.     BitBlt hMemDC, iDestX, iDestY, iMaskWidth, iMaskHeight, picMask.hdc, iM1SrcX, iM1SrcY, vbSrcPaint
  123.     BitBlt hMemDC, iDestX, iDestX, iMaskWidth, iMaskHeight, picMask1.hdc, iM2SrcX, iM2SrcY, vbSrcPaint
  124.     
  125. '------------------------------------------------------------
  126. 'Send it to a picture box, to make it visible
  127. 'This is only required in this demo
  128. '***
  129.     BitBlt frmMain.picCD.hdc, 0, 0, picBlank.ScaleWidth, picBlank.ScaleHeight, hMemDC, 0, 0, vbSrcCopy
  130.  
  131. '------------------------------------------------------------
  132. 'Examine the memory DC, and see if it contains any non white
  133. 'pixels. If so, set Collision = true and exit
  134. '------------------------------------------------------------
  135.     
  136.     blnCollision = False
  137.     For c = 0 To iMaskHeight - 1
  138.         For r = 0 To iMaskWidth - 1
  139.             If GetPixel(hMemDC, r, c) <> 16777215 Then
  140.                 blnCollision = True
  141.                 Exit For
  142.             Else
  143.             End If
  144.         Next
  145.         
  146.         If blnCollision = True Then
  147.             Exit For
  148.         End If
  149.         
  150.     Next
  151.  
  152. '------------------------------------------------------------
  153. 'Clean up resources
  154.     tmpObj = SelectObject(hMemDC, hPrevBMP)
  155.     tmpObj = DeleteObject(hPrevBMP)
  156.     tmpObj = DeleteDC(hMemDC)
  157.     
  158. '------------------------------------------------------------
  159.  
  160.     CollisionDetect = blnCollision
  161.     
  162. End Function
  163.  
  164.  
  165.